//管理用户数据相关
import {defineStore} from "pinia";
import {ref} from "vue";
import {
    findAddressByIdAPI,
    findMyAddressesAPI,
    removeDeliveryAddressAPI,
    saveDeliveryAddressAPI,
    updateDeliveryAddressAPI
} from "@/api/address";
export const useAddressStore=defineStore('address',()=>{

    let addressById=ref({})
    let addressListByUserId=ref({})


    const findAddressById=async (addressId) =>{
        const res=await findAddressByIdAPI(addressId)
        addressById.value=res
    }

    const findMyAddresses=async () =>{
        const res=await findMyAddressesAPI();
        addressListByUserId.value=res
    }

    const saveAddress = async (address) => {
        await saveDeliveryAddressAPI(address);

    }

    const updateAddress = async (address) => {
        await updateDeliveryAddressAPI(address);
    }

    const removeAddress = async (daId) => {
        await removeDeliveryAddressAPI(daId);
    }



    //3. 以对象的格式把state和action return
    return{
        addressById,
        addressListByUserId,
        findAddressById,
        findMyAddresses,
        saveAddress,
        updateAddress,
        removeAddress
    }
},{
    persist:true
})
